<?php
require 'BaseController.php';

class Admin_BannerController extends Admin_BaseController {
	
	private $_bannerModel;
	
		public function init(){
		parent::init();
		$auth = Zend_Auth::getInstance();
		$this->_identity = $auth->getIdentity();
		$this->view->identity = $this->_identity;
		$this->_bannerModel = new Model_Banner();
		}
		
public function indexAction(){
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckfinder/ckfinder.js');
		//khai báo session 
		$session = new Zend_Session_Namespace();
		$this->view->message  = $session->message;
		$session->__unset('message');
		
		//lấy  các banner
		$this->view->arrBanner = $this->_bannerModel->getAllBanners();
		
		$request = $this->getRequest();
		//lấy banner hiện tại nếu có
		$id = $request->getParam('id');
		if(is_numeric($id)){
			$banner = $this->_bannerModel->getBannerByID($id);
			if($banner != null) $this->view->banner = $banner;
		}
		//kiểm tra post
		if($request->isPost()){
			$name = $request->getParam('name');
			$image = $request->getParam('image');
			$banner_id = $request->getParam('id');
			$link = $request->getParam('link');
			
				if($this->_bannerModel->updateBanner($id, $name, $image, $link) > 0){
					$session->message = "<span class='success'>Sửa banner thành công</span>";
					$this->_redirector->gotoRoute(array('id'=>''), "admin_banner");
				}else{
					$session->message = "<span class='error'>Sửa banner không thành công</span>";
				}
			}
		
	}
}